/*
* Copyright (c) 2016 . All Rights Reserved.
*/
package com.cgmnx.admin.web.converter.vm;

import com.cgmnx.admin.dao.entity.rbac.ResourceEntity;
import com.cgmnx.admin.web.converter.Convert;
import com.cgmnx.admin.web.model.vm.MenuTreeVm;
import com.google.common.base.Function;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Multimap;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Component;

import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author: created  Date: 16/12/21 Time: 下午4:22
 * @version: 1.0
 */
@Component
public class MenuVmConverter implements Convert<ResourceEntity, MenuTreeVm> {

    private static final Integer MAIN_URI_FLAG = 1;

    private static List<MenuTreeVm> getChildren(Integer parentId,
                                                Map<Integer, ResourceEntity> idEntity,
                                                Map<Integer, Collection<Integer>> parentIds) {

        if (CollectionUtils.isEmpty(parentIds.get(parentId))) {
            return Collections.emptyList();
        }

        List<MenuTreeVm> uriTreeVos = Lists.newArrayList();

        for (Integer id : parentIds.get(parentId)) {

            uriTreeVos.add(getUriTreeVo(idEntity, parentIds, id));

        }

        return uriTreeVos;
    }

    private static MenuTreeVm getUriTreeVo(Map<Integer, ResourceEntity> idEntity, Map<Integer, Collection<Integer>> parentIds, Integer id) {

        MenuTreeVm vo = entity2Tree(idEntity.get(id));

        vo.setSub(getChildren(id, idEntity, parentIds));

        return vo;
    }

    private static MenuTreeVm entity2Tree(ResourceEntity entity) {
        MenuTreeVm vo = new MenuTreeVm();
        vo.setId(entity.getId());
        vo.setIconName(entity.getIcon());
        vo.setPath(entity.getUri());
        vo.setText(entity.getName());
        return vo;
    }

    public List<MenuTreeVm> entities2Tree(List<ResourceEntity> from) {

        if (CollectionUtils.isEmpty(from)) {
            return Collections.emptyList();
        }

        // 转成Map
        Map<Integer, ResourceEntity> idEntity = Maps.uniqueIndex(from, input -> {
            assert input != null;
            return input.getId();
        });

        Multimap<Integer, Integer> parentIdWithIds = ArrayListMultimap.create();

        // 将parentId对应的value查询出来
        for (ResourceEntity vo : from) {
            parentIdWithIds.put(vo.getParentId(), vo.getId());
        }

        return getChildren(MAIN_URI_FLAG, idEntity, parentIdWithIds.asMap());
    }

    @Override
    public MenuTreeVm convert(ResourceEntity t) {

        if (t == null) {
            return null;
        }

        MenuTreeVm vm = new MenuTreeVm();
        vm.setId(t.getId());
        vm.setIconName(t.getIcon());
        vm.setPath(t.getUri());
        vm.setText(t.getName());
        return vm;
    }

    @Override
    public List<MenuTreeVm> convert(List<ResourceEntity> t) {


        if (CollectionUtils.isEmpty(t)) {
            return Collections.emptyList();
        }

        return t.stream().map(input -> {

            if (input == null) {
                return null;
            }

            return convert(input);
        }).collect(Collectors.toList());


    }
}